var layer;
var index;
var size;

window.onload=function(){
	getContent();
};

function getContent() {
	$('#sample').show();
	$.post("userManage.php", {type: 'count'}, function(data) {
		$('#sample').hide();
		page("", data);
	});
}

function page(filter, count) {
	layui.use([ 'laypage', 'layer' ], function() {
		var laypage = layui.laypage;
		// 完整功能
		laypage.render({
			elem : 'page',
			count : count,
			layout : [ 'count', 'prev', 'page', 'next', 'limit', 'skip' ],
			jump : function(obj) {
				$('#content').html("");
				$('#sample').show();
				$.post("userManage.php",{index : obj.curr, size : obj.limit, filter : filter}, function(data) {
					index=obj.curr;
					size=obj.limit;
					$('#sample').hide();
					$('#content').html(data);
				});
			}
		});
	});
}

//筛选
function filtrate() {
	layui.use(
		[ 'layer', 'form' ],
		function() {
			var content = '<form class="layui-form"id="filtrateForm"onsubmit="return filter(this)"><div class="layui-form-item"><label class="layui-form-label">会员帐号</label><div class="layui-input-block"><input type="text"name="name"class="layui-input"></div></div><div class="layui-form-item"><div class="layui-input-block"><button class="layui-btn"lay-submit lay-filter="formDemo">立即提交</button></div></div></form>'; // 表单内容
			layer = layui.layer;
			layer.open({
				type : 1,
				title : '添加',
				skin : 'layui-layer-rim', // 加上边框
				area : [ '420px', '200px' ], // 宽高
				content : content
			});
			var form = layui.form;
			form.render();
		}
	);
}

function filter(form) {
	$('#content').html("");
	$('#sample').show();
	var filter = "";
	for (var i = 0; i < form.length; i++) {
		if (form[i].name != "") {
			filter += form[i].name + "=" + form[i].value + "&";
		}
	}
	layer.closeAll();
	$.post("userManage.php", {type : 'count'}, function(data) {
		$('#sample').hide();
		page(filter, data);
	});
	return false;
}

function keyDown(){
	if (window.event.keyCode==27){
		layer.closeAll();
	}
}

//全选
function checkAll() {
	var arrSon = document.getElementsByName("chkSon");
	var cbAll = document.getElementById("chkAll");
	var tempState = cbAll.checked;
	for (var i = 0; i < arrSon.length; i++) {
		if (arrSon[i].checked != tempState)
			arrSon[i].click();
	}
}

//批量删除
function Delete() {
	layui.use('layer', function() {
		var checkNum = 0; // 被选中按钮个数
		var layer = layui.layer;
		var deleteId = []; // 要删除的行id

		$('#content input').each(function() {
			if ($(this).prop('checked')) {
				deleteId+=($(this).parent().next().text())+",";
				checkNum++;
				$this = $(this);
			}
		});

		if (checkNum == 0) {
			layer.alert('请选中要删除的选项');
		} else {
			layer.confirm('确认要删除吗？', {
				btn : [ '确认', '取消' ]
			// 按钮
			}, function() {
				$('#content').html("");
				$('#sample').show();
				$.post('userManage.php?type=deleteAll', {
					deleteId : deleteId
				}, function(data) {
					$('#sample').hide();
					layer.msg(data);
					getContent();
				});
			}, function() {
			});
		}
	});
}

// 导入
function Import() {
	layui.use(
		'layer',
		function() {
			var content = '<div class="layui-upload"><button type="button"class="layui-btn layui-btn-normal"id="chooseFile">选择文件</button><button type="button"class="layui-btn"id="doUpload">开始上传</button><button type="button"class="layui-btn"onclick="download()">下载模板</button></div>'; // 表单内容
			layer = layui.layer;
			layer.open({
				type : 1,
				title : '导入',
				skin : 'layui-layer-rim', // 加上边框
				area : [ '450px', '100px' ], // 宽高
				content : content
			});
		}
	);
	layui.use('upload', function() {
		var layer = layui.layer;
		layui.jquery, upload = layui.upload;
		upload.render({
			elem : '#chooseFile',
			url : 'upload.php',
			accept : 'file', // 普通文件
			exts : 'xlsx|xls', // 只允许上传压缩文件
			auto : false,
			multiple : true,
			bindAction : '#doUpload',
			done : function(res) {
				if (res[0] == "false") {
					layer.msg(res[1]);
				} else {
					layer.closeAll();
					layer.msg(res[1]);
					getContent();
				}
			}
		});
	});
}

//下载模板
function download() {
	layui.use('layer', function(){
		var form = document.createElement("form");
		var input = document.createElement("input");
		input.type = "hidden";
		input.name = "type";
		input.value = "export";
		form.appendChild(input);
		var input = document.createElement("input");
		input.type = "hidden";
		input.name = "exportId";
		input.value = "downloadFile";
		form.appendChild(input);
		document.body.appendChild(form);
		form.method = "post";
		form.action = "userManage.php";
		form.submit();
		document.body.removeChild(form);
	});
}

//导出
function Export() {
	layui.use('layer', function() {
		var checkNum = 0; // 被选中按钮个数
		var layer = layui.layer;
		var exportId = []; // 要派奖的行id

		$('#content input').each(function() {
			if ($(this).prop('checked')) {
				exportId += ($(this).parent().next().text()) + ",";
				checkNum++;
				$this = $(this);
			}
		});

		var form = document.createElement("form");
		var input = document.createElement("input");
		input.type = "hidden";
		input.name = "type";
		input.value = "export";
		form.appendChild(input);
		var input = document.createElement("input");
		input.type = "hidden";
		input.name = "exportId";
		input.value = exportId;
		form.appendChild(input);
		document.body.appendChild(form);
		form.method = "post";
		form.action = "userManage.php";
		form.submit();
		document.body.removeChild(form);
	});
}

//用户资料
function userMsg() {
	layui.use('layer', function(){
		var layer = layui.layer;
		var content = '<form class="layui-form" id="userMsg" onsubmit="return checkUserMsg(this)"><div class="layui-form-item"><label class="layui-form-label">旧密码</label><div class="layui-input-block"><input type="password" name="oldPass" required  lay-verify="required" placeholder="输入旧密码" autocomplete="off" class="layui-input"></div></div><div class="layui-form-item"><label class="layui-form-label">新密码</label><div class="layui-input-block"><input type="password" name="newPass" required  lay-verify="required" placeholder="输入新密码" autocomplete="off" class="layui-input"></div></div><div class="layui-form-item"><label class="layui-form-label">重复新密码</label><div class="layui-input-block"><input type="password" name="reNewPass" required  lay-verify="required" placeholder="重复新密码" autocomplete="off" class="layui-input"></div></div><div class="layui-form-item"><div class="layui-input-block"><button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button></div></div></form>';
		form = '#userMsg';
		layer.open({
			type: 1,
			title: '编辑',
			skin: 'layui-layer-rim', //加上边框
			area: ['420px', '300px'], //宽高
			content: content
		});
	});
}

//组装提交的数据
function checkUserMsg(form){
	var newPass = form[1].value;
	var reNewPass = form[2].value;
	if(newPass != reNewPass){
		alert("新密码与重复新密码不一致！");
		return false;
	}
	var contentValue = $('#content').html();
	$('#content').html("");
	$('#sample').show();
	var data="";
	for(var i =0 ;i<form.length;i++){
		if(form[i].name != ""){
			data += form[i].name + "=" + form[i].value + "&";
		}
	}
	layer.closeAll();
	ajaxUser(data.substring(0,data.length-1), contentValue);
	return false;
}

//提交数据
function ajaxUser(data, contentValue) {
	$.ajax({
		url : "changePassword.php",
		type : "post",
		data : data,
		contentType: "application/x-www-form-urlencoded; charset=utf-8",
		success : function(data) {
			$('#sample').hide();
			if(data[0]=="false"){
				layer.msg(data[1]);
				$('#content').html(contentValue);
			} else {
				layer.msg(data);
				$('#content').html(contentValue);
			}
		},
		error:function(data){
			$('#sample').hide();
			alert("请求异常");
	    },
	});
}
